package com.google.code.rgbgoesbetween.gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.code.rgbgoesbetween.R;
import com.google.code.rgbgoesbetween.data.Item;
import com.google.code.rgbgoesbetween.data.Item.ItemStatus;
import com.google.code.rgbgoesbetween.data.Profile;
import com.google.code.rgbgoesbetween.services.DataProvider;
import com.google.code.rgbgoesbetween.services.SessionManager;
/**
 * GUI that allows users to report their lost/found items.
 * @author Patrick
 *
 */
public class ReportItemActivity extends Activity implements OnClickListener {

	private DataProvider dataProvider = DataProvider.getDataProvider();
	
	@SuppressWarnings("unused")
	private TextView notificationsView;
	private EditText itemNameView;
	private Spinner lostFoundSpinner;
	private EditText descriptionView;
	private DatePicker dateView;
	private EditText locationView;
	private EditText tagsView;
	private Button reportButton;

	/**
	 * Initializes the GUI and sets corresponding listeners. Initializes session manager and data provider if necessary./
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report);
		dataProvider.init(this);
	    SessionManager.getSessionManager().init();
        			
        notificationsView = (TextView) 	findViewById(R.id.notifications_field);
        itemNameView 	  = (EditText) 	findViewById(R.id.item_name_field);
        lostFoundSpinner  = (Spinner)  	findViewById(R.id.item_status_spinner);
        descriptionView	  = (EditText) 	findViewById(R.id.description_input_field);
        dateView   	  	  = (DatePicker)findViewById(R.id.date_selector);
        locationView 	  = (EditText)	findViewById(R.id.location_input_field);
        tagsView		  = (EditText)	findViewById(R.id.tags_input_field);
        reportButton 	  = (Button)	findViewById(R.id.report_button);
        
        reportButton.setOnClickListener(this);
	}

	/**
	 * Inflate the menu; this adds items to the action bar if it is present.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_home, menu);
		return true;
	}
	
	/**
	 * Handles click events like the "report button". Creates an item in the data store withthe given information.
	 */
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.report_button) {
			GregorianCalendar dateCreated = new GregorianCalendar();
			dateCreated.set(dateView.getYear() - 1900, 
					dateView.getMonth(), dateView.getDayOfMonth());
			
			Item i = new Item();
			i.setName(itemNameView.getText().toString());
			i.setStatus(ItemStatus.values()[lostFoundSpinner.getSelectedItemPosition()]);
			i.setDescription(descriptionView.getText().toString());
			i.setDateRange(new Calendar[]{dateCreated});
			i.setLocation(locationView.getText().toString());
			i.setTags(tagsView.getText().toString().split(","));
			i.setReporter(SessionManager.getSessionManager().getProfile());
		
			Item res = dataProvider.createItem(i);
			Log.d("REPORT_ITEM_ACTIVITY", "Item: " + i);
			if (res == null) {
				Toast.makeText(this, "Error adding item", Toast.LENGTH_LONG).show();
				return;
			} else {
				Profile profile = SessionManager.getSessionManager().getProfile();
				ArrayList<Integer> ids = new ArrayList<Integer>();
				ids.addAll(profile.getItemIDList());
				ids.add(i.getId());
				profile.setItemIDList(ids);
				SessionManager.getSessionManager().updateProfile(profile);
				
				Log.d("REPORT_ITEM_ACTIVITY", Arrays.toString(profile.getItemIDList().toArray()));
				
				Toast.makeText(this, "Success adding item", Toast.LENGTH_LONG).show();
				this.finish();
			}
		}
	}

}
